package com.example.springboottestgit.controller.main;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: ZZZ
 */
public class FindText {
    static int n = 0;
    static int m = 0;
    static int z = 0;
    static int key_word_count = 3;


    static String FILE_PATH = "C:\\Users\\user\\Desktop\\\\处理中\\utf-8政务数据集";
    static String OLD_FILE_NAME = "utf-8政务数据集";
    static String NEW_FILE_NAME = "经济政策相关文件";


    static Map<String, Integer> map = new HashMap<>();


    public static void main(String[] args) throws IOException {

        LocalDateTime startTime = LocalDateTime.now();

        //关键字初始化
        map.put("经济政策", 0);
        map.put("财政政策", 0);
        map.put("财政拨款", 0);
        map.put("财务支出", 0);
        map.put("经营收入", 0);
        map.put("国民经济", 0);
        map.put("社会发展", 0);
        map.put("绿色发展", 0);
        map.put("经济区", 0);
        map.put("区域经济", 0);
        map.put("工程项目", 0);
        map.put("工程建设", 0);
        map.put("项目建设", 0);
        map.put("设施建设", 0);
        map.put("基础设施", 0);
        map.put("城乡建设", 0);
        map.put("建设内容", 0);
        map.put("招商引资", 0);

        map.put("港口", 0);
        map.put("改革", 0);
        map.put("贸易", 0);
        map.put("产业", 0);
        map.put("货币", 0);
        map.put("工业", 0);
        map.put("投资", 0);


        searchFile(new File(FILE_PATH));
        System.out.println("================================");
        System.out.println("文件总数：" + n);
        System.out.println("关键字名字数：" + key_word_count);
        System.out.println("关键字命中文件总数：" + m);

        map.forEach((key, value) -> {
            z = z + value;
            System.out.println(key + " " + value);
        });
        LocalDateTime endTime = LocalDateTime.now();
        Duration between = Duration.between(startTime, endTime);
        System.out.println("耗时秒：" + between.getSeconds());
        System.out.println("关键字命中文件总数验证：" + z);

    }

    public static void searchFile(File dir) throws IOException {
        boolean found = false;//如果没找到要给出回应
        if (dir != null && dir.isDirectory()) {
            File[] files = dir.listFiles();//用数组遍历文件夹中的文件
            if (files != null && files.length > 0) {
                for (File file : files) {
                    if (file.isFile()) {
                        n++;
                        String text = getTextContent(file.getPath());
                        int count = 0;
                        for (Map.Entry<String, Integer> entry : map.entrySet()) {
                            String key = entry.getKey();
                            Integer value = entry.getValue();
                            if (text.contains(key)) {
                                count++;
                                map.put(key, value + 1);
                                if (count >= key_word_count) {
                                    m++;
                                    filesCopy(file);
                                    break;
                                }
                            }
                        }
                    } else {
                        searchFile(file);//递归运算
                    }
                }
            }
        } else {
            System.out.println("当前搜索的位置不是文件夹！");
        }
    }

    public static void filesCopy(File file) {
        //创建新的文件目录
        String newTextPath = file.getPath().replace(OLD_FILE_NAME, NEW_FILE_NAME);
        File makeFile = new File(newTextPath.replace(file.getName(), ""));
        if (!makeFile.exists()) {
            makeFile.mkdirs();
        }

        Path sourcePath = Paths.get(file.getPath()); // 源文件路径
        Path destinationPath = Paths.get(newTextPath);
        try {
            Files.copy(sourcePath, destinationPath, StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }

    }

    public static String getTextContent(String path) throws IOException {
        List<KY> list = new ArrayList<>();
        FileReader reader = new FileReader(path);
        BufferedReader bufferedReader = new BufferedReader(reader);
        StringBuilder stringBuilder = new StringBuilder();
        String line;

        while ((line = bufferedReader.readLine()) != null) {
            stringBuilder.append(line);
        }
        bufferedReader.close();
        return stringBuilder.toString();
    }

}
